/**
 * @file InetAddress.cc
 * @author zziywang (zziywang@163.com)
 * @brief { life is too short to learn cpp! }
 * @version 1.0
 * @date 2022-05-07
 *
 * @copyright Copyright (c) 2022
 *
 */

#include "InetAddress.hh"
#include <string.h>

namespace snow
{

InetAddress::InetAddress(unsigned short port)
{
    memset(&addr_, 0, sizeof(addr_));
    addr_.sin_addr.s_addr = inet_addr("0.0.0.0");
    addr_.sin_port = htons(port);
    addr_.sin_family = AF_INET;
}
InetAddress::InetAddress(const string &ip, unsigned short port)
{
    memset(&addr_, 0, sizeof(addr_));
    addr_.sin_addr.s_addr = inet_addr(ip.c_str());
    addr_.sin_port = htons(port);
    addr_.sin_family = AF_INET;
}
InetAddress::InetAddress(const struct sockaddr_in &addr) : addr_(addr) {}

string InetAddress::getIp() const
{
    return string(::inet_ntoa(addr_.sin_addr));
}
unsigned short InetAddress::getPort() const
{
    return ::ntohs(addr_.sin_port);
}

} // namespace snow